package com.tools.demo.image;
import net.ifok.image.image4j.codec.ico.ICOEncoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ImageTools {

    public static void main(String[] args) throws Exception {
        //File file = new File("C:/Users/yys/Desktop/gggh.jpg");
        InputStream in = new FileInputStream("/Users/admin/Downloads/test/test.jpeg");
        BufferedImage bufferedImage = ImageIO.read(in);
        if(bufferedImage == null){
            System.out.println("File not find");
            return;
        }
        List<BufferedImage> icons = new ArrayList<>();
        icons.add(getScaledInstance(bufferedImage, 16, 16));
        icons.add(getScaledInstance(bufferedImage, 32, 32));
        icons.add(getScaledInstance(bufferedImage, 64, 64));
        icons.add(getScaledInstance(bufferedImage, 128, 128));
        ICOEncoder.write(icons,new File("/Users/admin/Downloads/test/logo.ico"));
    }


    /**
     *
     * @param imgName 源图名称
     * @param imgPath 图片路径
     * @param imgFormat 要修改成的图片格式
     * @param imgNewName 新图片名称
     * @return
     */
    public static Map<String,Object> updateImgs(String imgName,String imgPath,String imgFormat,String imgNewName){
        Map<String,Object> map = new HashMap<>();
        try {
            InputStream in = new FileInputStream(imgPath+imgName);
            map = updateImgs(in,imgPath,imgFormat,imgNewName);
        }catch (Exception e){
            map.put("status",false);
            System.out.println("更改图片格式异常："+e.getMessage());
            return map;
        }

        return map;
    }

    /**
     *
     * @param in 原图片流
     * @param imgPath 图片路径
     * @param imgFormat 要修改成的图片格式
     * @param imgNewName 新图片名称
     * @return
     */
    public static Map<String,Object> updateImgs(InputStream in,String imgPath,String imgFormat,String imgNewName){
        Map<String,Object> map = new HashMap<>();
        try {
            BufferedImage bufferedImage = ImageIO.read(in);
            if(bufferedImage == null){
                System.out.println("File not find");
                map.put("status",false);
                return map;
            }
            List<BufferedImage> icons = new ArrayList<>();
            icons.add(getScaledInstance(bufferedImage, 16, 16));
            icons.add(getScaledInstance(bufferedImage, 32, 32));
            icons.add(getScaledInstance(bufferedImage, 64, 64));
            icons.add(getScaledInstance(bufferedImage, 128, 128));
            ICOEncoder.write(icons,new File(imgPath+imgNewName));

            map.put("status",true);
            map.put("imgUrl",imgPath+imgNewName);
        }catch (Exception e){
            map.put("status",false);
            System.out.println("更改图片格式异常："+e.getMessage());
            return map;
        }

        return map;
    }

    public static BufferedImage getScaledInstance(BufferedImage bufferedImage, int toWidth, int toHeight){
        Image scaledInstance = bufferedImage.getScaledInstance(toWidth, toHeight, 0);
        BufferedImage newBufferedImage = new BufferedImage(toWidth,
                toHeight, BufferedImage.TYPE_INT_RGB);
        newBufferedImage.createGraphics().drawImage(scaledInstance, 0, 0, Color.WHITE, null);
        return newBufferedImage;
    }
}
